package com.vecspace.security.proxy.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.vecspace.security.proxy.dayu.SSORestUtil;
import com.vecspace.sso.entity.AppFuncUrl;
import com.vecspace.sso.entity.AppFunctionAuth;
import com.vecspace.sso.entity.SSOTenant;
import com.vecspace.sso.entity.SSOTenantEx;
import com.vecspace.sso.entity.SSOUser;
import com.vecspace.sso.entity.UserBase;
import com.vecspace.utils.Convert;
import com.vecspace.web.HttpContextThreadLocal;

@Service
public class AuthorizeServiceDayuImpl implements AuthorizeService{

	@Override
	public String login(String loginId, String password) throws Exception {
		HttpServletRequest req = HttpContextThreadLocal.getHttpContext().getHttpRequest();
		HttpSession session = req.getSession();
		Object randImg = session.getAttribute("rand");
		LoginManager.clearSession(req);//登录之后，要清除session，否则session里面可能会带上上次登录用户的信息。清除session之后要重新登录，在session中记录登录信息。	
		session = req.getSession();
		if(randImg != null) session.setAttribute("rand", randImg);//清除session之后，再恢复session中的randImg，否则验证码出错

		String loginToken = SSORestUtil.InvokeT(restTemplate, String.class, loginServiceUrl, "login",  new String[] {"loginId", "password"}, loginId, password);
		
		return loginToken;
	}
	
	@Override
	public SSOUser getUser(String loginId) throws Exception {
		Map<?,?> userObj = SSORestUtil.InvokeT(restTemplate, HashMap.class, loginServiceUrl, "getLogonUser",  new String[] {});
		SSOUser user = new SSOUser();
		user.setId(Convert.toLong(userObj.get("id"), -1L));
		user.setLoginId(Convert.toString(userObj.get("loginId"), ""));
		user.setJobNumber(Convert.toString(userObj.get("jobNumber"), ""));
		user.setUserName(Convert.toString(userObj.get("userName"), ""));
		user.setEmail(Convert.toString(userObj.get("email"), ""));
		user.setSex(Convert.toString(userObj.get("sex"), ""));
		user.setJobTitle(Convert.toString(userObj.get("jobTitle"), ""));
		user.setJobPost(Convert.toString(userObj.get("jobPost"), ""));
		user.setMobile(Convert.toString(userObj.get("mobile"), ""));
		user.setBirthday(Convert.toDate(userObj.get("birthday"), new Date(0)));
		user.setMacAddress(Convert.toString(userObj.get("macAddress"), ""));
		user.setIpAddress(Convert.toString(userObj.get("ipAddress"), ""));
		user.setDistrictCode(Convert.toString(userObj.get("districtCode"), ""));
		user.setNativePlace(Convert.toString(userObj.get("nativePlace"), ""));
		user.setEthnic(Convert.toString(userObj.get("ethnic"), ""));
		user.setHighestEducation(Convert.toString(userObj.get("highestEducation"), ""));
		user.setTel(Convert.toString(userObj.get("tel"), ""));
		user.setAddress(Convert.toString(userObj.get("address"), ""));
		user.setHighestEducation(Convert.toString(userObj.get("highestEducation"), ""));
		user.setAuthTag(Convert.toInteger(userObj.get("authTag"), -1));
		user.setJobState(Convert.toInteger(userObj.get("jobState"), -1));
		user.setDeleteFlag(Convert.toInteger(userObj.get("deleteFlag"), -1));
		return user;
	}

	@Override
	public UserBase getUserBaseInfo(String loginId) throws Exception {
		Map<?,?> userObj = SSORestUtil.InvokeT(restTemplate, HashMap.class, loginServiceUrl, "getLogonUser",  new String[] {});
		UserBase user = new UserBase();
		user.setId(Convert.toLong(userObj.get("id"), -1L));
		user.setDeptId(Convert.toLong(userObj.get("deptId"), -1L));
		user.setDeptName(Convert.toString(userObj.get("deptName"), ""));
		user.setLoginId(Convert.toString(userObj.get("loginId"), ""));
		user.setUserName(Convert.toString(userObj.get("userName"), ""));
		user.setEmail(Convert.toString(userObj.get("email"), ""));
		user.setSex(Convert.toString(userObj.get("sex"), ""));
		user.setMobile(Convert.toString(userObj.get("mobile"), ""));
		user.setMacAddress(Convert.toString(userObj.get("macAddress"), ""));
		user.setDistrictCode(Convert.toString(userObj.get("districtCode"), ""));
		return user;
	}

	@Override
	public boolean checkLoginToken(String loginToken) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean resetPassword(String loginId, String oldPassword, String newPassword) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<AppFunctionAuth> getAuthFunctionTree(Long userId, String parentFuncCode) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AppFunctionAuth> getAuthorizedChildrenFunctions(Long userId, Long tenantId, String appKey,
			String parentFuncCode) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AppFunctionAuth> getAllAuthByTree(Long userId, Long tenantId, String appKey, String parentFuncCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AppFuncUrl> getAuthFunctionUrlList(Long userId, Long tenantId, String appKey, String parentFuncCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SSOTenant getTenantByCustomId(String customTenantId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SSOTenantEx> getAccessableTenants(Long userId, String tenantType) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isAccessable2Tenant(Long userId, String customTenantId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isInParentTenant(Long userId, String customTenantId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<UserBase> getAllUsers() {
		// TODO Auto-generated method stub
		return null;
	}

	@Autowired
	private RestTemplate restTemplate;
	
	@Value("sso.loginServiceUrl")
	private String loginServiceUrl;
	
	@Value("sso.authServiceUrl")
	private String authServiceUrl;
}
